package me.zhengjie.modules.transfer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.classgraph.ResourceList;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.account.factory.service.AccountComputer;
import me.zhengjie.modules.account.mapper.AccountCommonMapper;
import me.zhengjie.modules.account.service.AccountAc50Service;
import me.zhengjie.modules.account.service.AccountService;
import me.zhengjie.modules.basic.domain.BasicAc02;
import me.zhengjie.modules.basic.service.BasicAc02Service;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.modules.transfer.factory.service.*;
import me.zhengjie.modules.transfer.service.TransferIc90Service;
import me.zhengjie.modules.transfer.service.TransferOutService;
import me.zhengjie.tool.ListUtil;
import me.zhengjie.tool.MyMap;
import me.zhengjie.tool.NPETool;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class TransferOutServiceImpl implements TransferOutService {

    private final TransferInformationIc90Service transferInformationIc90Service;
    private final TransferInformationService transferInformationService;
    private final TransferPaymentService transferPayment;
    private final TransferInformationIc66Service transferInformationIc66Service;
    private final TransferInformationIc74Service transferInformationIc74Service;
    private final TransferInformationIc60Service transferInformationIc60Service;
    private final TransferInformationIc61Service transferInformationIc61Service;
    private final AccountComputer accountComputer;
    private final TransOutDealService transOutDealService;
    private final AccountCommonMapper accountCommonMapper;
    private final TransferIc90Service transferIc90Service;
    private final TransferContactService transferContact;
    private final BasicAc02Service basicAc02Service;
    private final AccountAc50Service accountAc50Service;
    private final AccountService accountService;
    private final SequenceService sequenceService;
    private final TransferProcessService transferProcessService;
    private final TransferPersonService transferPersonService;
    private final  BusinessLogService businessLogService;



    /**
     * 计息缴费集合
     */
    protected List<Map> paymentListCal;

    /**
     * 信息表缴费集合
     */
    protected List<Map> paymentListXXB;

    protected String aab359;//参保地
    /**
     * 转出登记
     * @param map
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map transferOutSave(MyMap map) throws IOException {
        this.aab359=map.getAsString("aab359");

        //查询参保关系id
        Map basic = getBasic(map);
        map.putAll(basic);
        String aic113=map.getAsString("aic113");
        if(!Constant.AIC113_9_ZYNJZY.equals(aic113)){//职业年金转移不计算养老账户

            //处理账户
            Map account = getAccount(map);

            map.putAll(account);
        }



        //转出登记的业务日志号 回退数据也依据这个来回退

        map.put("aaz002",sequenceService.nextSequence(new Sequence("aaz002")));

        map.put("aae016",Constant.AAE016_1_SHTG);



        //2、保存联系函
        transferContact.TransferContactOut(map);

        map.put("aab359",this.aab359);//联系函保存成功会把经办机构清空了




        //3、信息表保存
        switch (aic113){
            case Constant.AIC113_1_QYZGYLBXZDNZY:
                map.put("aaa121", YHConstants.AAA121_040101);
                Map map1 = transferInformationIc90Service.transOutInfo(map);
               if(Constant.AAE473_1_YBZH.equals(map1.get("aae473"))) {
                   map.put("aae803",Constant.AAE803_T0102_ZYJXXXB);
               }else{
                   map.put("aae803",Constant.AAE803_T0502_ZRDFQDLSZHZ);
               }
                break;
            case Constant.AIC113_2_QYZGYLBXZDZJGSYDWYLBXZD:
                map.put("aaa121", YHConstants.AAA121_040101);
                 transferInformationIc66Service.transOutInfo(map);
                map.put("aae803",Constant.AAE803_T1102_JGYLBXZYXXB);
                break;
            case Constant.AIC113_3_JGSYDWYLBXZDZQYZGYLBXZD:
                map.put("aaa121", YHConstants.AAA121_040101);
                transferInformationIc66Service.transOutInfo(map);
                map.put("aae803",Constant.AAE803_T1102_JGYLBXZYXXB);
                break;
            case Constant.AIC113_6_JGSYDWYLBXZDNZY:
                map.put("aaa121", YHConstants.AAA121_040101);
                transferInformationIc66Service.transOutInfo(map);
                map.put("aae803",Constant.AAE803_T1102_JGYLBXZYXXB);
                break;
            case Constant.AIC113_4_QYZGYLBXZDZCXJMJBYLZD:
                map.put("aaa121", YHConstants.AAA121_040101);
                transferInformationIc74Service.transOutInfo(map);
                map.put("aae803",Constant.AAE803_T0802_CZZGYLBXZRCXJMYLBXXXB);
                break;
            case Constant.AIC113_9_ZYNJZY:
                map.put("aaa121", YHConstants.AAA121_040102);
                transferInformationIc60Service.transOutInfo(map);
                map.put("aae803",Constant.AAE803_T1302_JGSYYLBXZYNJZYXXB);
                break;
            case Constant.AIC113_10_ZYNJZY:
                map.put("aaa121", YHConstants.AAA121_040102);
                transferInformationIc61Service.transOutInfo(map);
                map.put("aae803",Constant.AAE803_T1302_JGSYYLBXZYNJZYXXB);
                break;
            default:
                throw new IllegalArgumentException("没有获取到对应的转出类型！");
        }


        //4、终止账户
        accountService.terminationAccount(map);

        //5、终止参保 因为涉及多个账户 转出登记不终止参保（查询账户 还存在有效的就不终止 不存在就终止）
         accountService.terminationBasic(map);



        //6、生成转出人员支付表
        transferPayment.transferoutCreatBills(map);

        //7、记录日志信息
        BusinessLog businessLog = BusinessLog.singleLog(map);
        businessLogService.save(businessLog);


        return new HashMap();
    }

    /**
     * 转出登记预览
     * @param map
     * @return
     */
    @Override
    public Map transferOutPreview(MyMap map) {


        //查询参保关系id
        Map basic = getBasic(map);
        map.putAll(basic);
        String aic113=map.getAsString("aic113");
        if(!Constant.AIC113_9_ZYNJZY.equals(aic113)){//职业年金转移不计算养老账户

            Map account = getAccount(map);

            map.putAll(account);
        }



        Map map1 =new HashMap<>();
                //3、信息表保存
        switch (aic113){
            case Constant.AIC113_1_QYZGYLBXZDNZY:

                 map1 = transferInformationIc90Service.informationPreview(map);
                if(Constant.AAE473_1_YBZH.equals(map1.get("aae473"))) {
                    map.put("aae803",Constant.AAE803_T0102_ZYJXXXB);
                }else{
                    map.put("aae803",Constant.AAE803_T0502_ZRDFQDLSZHZ);
                }
                break;
            case Constant.AIC113_2_QYZGYLBXZDZJGSYDWYLBXZD:
                map1 =   transferInformationIc66Service.informationPreview(map);
                map.put("aae803",Constant.AAE803_T1102_JGYLBXZYXXB);
                break;
            case Constant.AIC113_3_JGSYDWYLBXZDZQYZGYLBXZD:
                map1 =  transferInformationIc66Service.informationPreview(map);
                map.put("aae803",Constant.AAE803_T1102_JGYLBXZYXXB);
                break;
            case Constant.AIC113_6_JGSYDWYLBXZDNZY:
                map1 =  transferInformationIc66Service.informationPreview(map);
                map.put("aae803",Constant.AAE803_T1102_JGYLBXZYXXB);
                break;
            case Constant.AIC113_4_QYZGYLBXZDZCXJMJBYLZD:
                map1 =   transferInformationIc74Service.informationPreview(map);
                map.put("aae803",Constant.AAE803_T0802_CZZGYLBXZRCXJMYLBXXXB);
                break;
            case Constant.AIC113_9_ZYNJZY:
                map1 =   transferInformationIc60Service.informationPreview(map);
                map.put("aae803",Constant.AAE803_T1302_JGSYYLBXZYNJZYXXB);
                break;
            case Constant.AIC113_10_ZYNJZY:
                map1 = transferInformationIc61Service.informationPreview(map);
                map.put("aae803",Constant.AAE803_T1302_JGSYYLBXZYNJZYXXB);
                break;
            default:
                throw new IllegalArgumentException("没有获取到对应的转出类型！");
        }

        return map1;
    }

    /**
     * 转出登记回退
     * @param map
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map transferOutRevokeSave(MyMap map) {

        NPETool.isEmptyThrowException(map.getAsString("aaz002"), "业务日志ID");
        String aaz002=   map.getAsString("aaz002");

        List<Map<String, Object>> ac10List = transferProcessService.getProcessTransfer(map.getAsString("aaz002"));
        if(ac10List.size()>0) {

            String bac617 = ac10List.get(0).get("bac617").toString();

            map.put("aic113", bac617);
            map.put("aaz159", ac10List.get(0).get("aaz159").toString());

            List<Map> maps = transferPayment.querytransferoutBills(map);
            if (maps.size() > 0) {
                if (Constant.BAD006_04_YSSF.equals(maps.get(0).get("aae456").toString())) {
                    throw new IllegalArgumentException("支付成功 不允许回退！");
                }
            }


            //撤销联系函
            transferContact.TransferContactRevoke(map);

            //撤销信息表
            switch (bac617) {
                case Constant.AIC113_1_QYZGYLBXZDNZY:
                    map.put("aae803",Constant.AAE803_T0102_ZYJXXXB);
                    transferInformationIc90Service.transOutRevoke(aaz002);
                    break;
                case Constant.AIC113_2_QYZGYLBXZDZJGSYDWYLBXZD:
                    map.put("aae803", Constant.AAE803_T1102_JGYLBXZYXXB);
                    transferInformationIc66Service.transOutRevoke(aaz002);
                    break;
                case Constant.AIC113_3_JGSYDWYLBXZDZQYZGYLBXZD:
                    map.put("aae803", Constant.AAE803_T1102_JGYLBXZYXXB);
                    transferInformationIc66Service.transOutRevoke(aaz002);
                    break;
                case Constant.AIC113_6_JGSYDWYLBXZDNZY:
                    map.put("aae803", Constant.AAE803_T1102_JGYLBXZYXXB);
                    transferInformationIc66Service.transOutRevoke(aaz002);
                    break;
                case Constant.AIC113_4_QYZGYLBXZDZCXJMJBYLZD:
                    map.put("aae803", Constant.AAE803_T0802_CZZGYLBXZRCXJMYLBXXXB);
                    break;
                case Constant.AIC113_9_ZYNJZY:
                    transferInformationIc60Service.transOutRevoke(aaz002);
                    map.put("aae803", Constant.AAE803_T1302_JGSYYLBXZYNJZYXXB);
                    break;
                case Constant.AIC113_10_ZYNJZY:
                    transferInformationIc61Service.transOutRevoke(aaz002);
                    map.put("aae803",Constant.AAE803_T1302_JGSYYLBXZYNJZYXXB);
                    break;
                default:
                    throw new IllegalArgumentException("没有获取到对应的转出类型！");
            }


            //参保撤销
            accountService.terminationBasicRevoke(map);

            //账户撤销
            accountService.terminationAccountRevoke(map);

            //支付表撤销
            transferPayment.transferoutCreatBillsRevoke(map);

            //撤销流程
            transferProcessService.deleteProcessTransfer(aaz002);
        }
        return null;

        }




    /**
     * 转出账户处理 获取明细处理账户
     * @param map
     */
    private Map getAccount(MyMap map){

        HashMap resultMap = new HashMap<>();
        //获取缴费明细

        HashMap query = new HashMap<>();
        query.put("aaz159",map.getAsString("aaz159"));
        query.put("tyjrzhdy", "1");
        query.put("zzzhdy", "1");//终止账户也需要查询打印
        query.put("ynPc06", "1");
        query.put("aae820", Constant.AAC062_1_RYXCB);
        List<Map> paymentList = accountCommonMapper.queryAc08(query);
        map.put("paymentList",paymentList);
        //转出账户处理
        Map map1 = transOutDealService.dealAccount(paymentList);
        map.putAll(map1);
       //调用计息  不保存库表
        Map map2 = accountComputer.computerAccountNotSave(map);


        //考虑到存在改革前试点账户和正常账户 转出生成信息表的时候 有效明细和台账过滤只取正常账户的
        List<Map> ac50s = (List<Map>) map2.get("ac50");
        List<Map> ac51s = (List<Map>) map2.get("ac51");

        List<Map> newac50s = ac50s.stream().filter(m -> Constant.AAC062_1_RYXCB.equals(m.get("aac062"))).collect(Collectors.toList());
        List<String> aae116s = ListUtil.listMap2ListString(newac50s,"aaz116");

        List<Map> newac51s = ac51s.stream().filter(m -> aae116s.contains(m.get("aaz116"))).collect(Collectors.toList());
       map.put("ac51",newac51s);
       map.put("ac50",newac50s);




        resultMap.putAll(map);
        return resultMap;
    }

    private Map getBasic(MyMap map){

        MyMap myMap = transferPersonService.xxbAae140Aae241(map.getAsString("aic113"));


        HashMap queryMap = new HashMap<>();
        queryMap.put("aac001",map.getAsString("aac001"));
    //    queryMap.put("aae140",myMap.getAsString("aae140"));
        queryMap.put("aac008",Constant.AAC008_1_ZCCB);
        queryMap.put("aac031s", Arrays.asList(Constant.AAC031_1_CBJF,Constant.AAC031_2_ZTJF));

        List<Map> list = accountCommonMapper.getAc02(queryMap);
        if(list.size()<=0){
            throw new IllegalArgumentException("获取人员参保关系异常");
        }
        HashMap resultMap = new HashMap<>();
        resultMap.put("aaz159",list.get(0).get("aaz159"));
        return resultMap;
    }
}
